<?php
require_once( 'Zend/Controller/Action.php');
require_once( 'Zend/Db.php' );
require_once( 'Zend/Db/Table.php' );
require_once( 'Zend/Db/Table/Abstract.php' );
require_once( 'models/Paciente.php');
require_once( 'models/Es_Medico.php');
require_once( 'models/Revisa.php');
require_once( 'models/Consulta.php');
require_once( 'models/Cola_De_Espera.php');
require_once( 'modules/default/Form/Form.php');
require_once( 'IndexController.php');
require_once( 'Zend/Exception.php');
require_once( 'InitController.php');
require_once( 'Zend/Dojo.php');
require_once( 'Zend/Dojo/Form.php');
require_once( 'Zend/Dojo/Form/Element/DateTextBox.php');
require_once( 'Zend/Dojo/View/Helper/DateTextBox.php');
require_once( 'Zend/Dojo/View/Helper/Dijit.php');
require_once( 'Zend/Dojo/View/Helper/Dojo/Container.php');
require_once( 'Zend/Dojo/Form/Decorator/DijitElement.php');
require_once( 'Zend/View.php');

class ColaController extends InitController{
	
	public function indexAction(){
		 $this->view->title = 'Medisoft - Ingresar Paciente a Cola de Espera';  
    	$this->view->date = $this->getQueueForm();
	}
	
	public function insAction(){
		$this->view->title = 'Medisoft - Ingresar Paciente a Cola de Espera';
    	$request = $this->getRequest();
    	
    	 if ($request->isPost()) {
    	 	
/*    	    $form = $this->getQueueForm();
        	if (!$form->isValid($request->getPost())) {
        	    
        	    $this->view->form = $form;
        	    $this->view->form->setAction("/cola")->SetMethod('post');
        	    return $this->render('index'); // re-render the form
        	}
*/
    	 	$verifyDNI = $_POST['dni'];
    	 	if($verifyDNI != ''){
    	 		
    	 		$cola = new COLA_DE_ESPERA;
    	 		$cola->pacdni = $_POST['dni'];
    	 		$cola->meddni = $_POST['medico'];
    	 		$cola->horallegada = new Zend_Date(Zend_Date::ISO_8601);
    	 		$cola->horallegada = $cola->horallegada->toString('YYYY-MM-dd HH:mm:ss');

    	 		
    	 		$res = $cola->insToCola();
    	 	    if ($res == 0)
                {
                    $this->view->message = 'No se pudo Ingresar el paciente a la cola';
                }else{
                	$this->view->message = 'Se ha ingresado con éxito';
                }
    	 	}else{
    	 		$this->view->message = 'Debe Ingresar el DNI de un paciente para poder ingresarlo.';
    	 		$this->render('ins');
    	 		return;
    	 	}
    	 	return $this->render('ins');
    	 }
	}
	
	private function getQueueForm(){
		$form = new Zend_Form();
		$form->setAction("/cola/ins")->setMethod('post');
		
		$decoradores = array(
		'ViewHelper',
		'Errors',
     	array('HtmlTag', array('tag' => 'div', 'class' =>'b')),
     	array('Label', array('requiredSuffix'=>'*')),
     	//array('Fieldset', array('tag' =>'fset'))
     	//array('ViewScript', array('viewScript' => 'decorador-contenedor.phtml', 'placement' => false))
		);
		
		
		$med_table = new ES_MEDICO();
		$this->view->meds = $med_table->listallMedicos();
		
	 	$medarray = array();
        foreach ($this->view->meds as $meds)
        {
        	$medarray[$meds->PEDNI] = $meds->PENOMBRE. " ". $meds->APELLIDO . " (" . $meds->PEDNI . ")" ;
        }
        
        $medsname = $form->createElement('select', 'medico', array('label' => 'Medico',  'value' => 'medico', 'multiOptions' => $medarray));
        
        $pacdni = $form->createElement('text', 'dni', array('label' => 'Ingrese el DNI del paciente a ingresar:'));
		$pacdni->addValidator('alnum')
		         ->addValidator('regex', false, array('/^[0-9]+/'))
		         ->addValidator('stringLength', false, array(1, 10))
		         ->setRequired(true)
		         ->addFilter('StringToLower');
		         
		$submit = $form->createElement('submit', 'seleccionar', array('label' => 'Ingresar'));
		         
		         
		$form->addElement($medsname)
             ->addElement($pacdni)
                     //->addElement('hidden', 'auxdni', array('values' => ''))
             ->addElement($submit);
        return $form;
        
	}
}